package com.example.deepschool.service.Report;

import com.example.deepschool.Model.Pagination;
import com.example.deepschool.Model.User;
import com.example.deepschool.exception.CustomException;
import com.example.deepschool.repositories.ReportRepository;
import com.example.deepschool.util.PermissionsControl;
import com.example.deepschool.util.Redis;
import lombok.SneakyThrows;

import java.util.Objects;

import static com.example.deepschool.util.Verify.VerifyReportAdd;
import static com.example.deepschool.util.Verify.VerifyReportUpdate;

public class ReportImpl implements Report {
	
	private final ReportRepository reportRepository;
	
	public ReportImpl (ReportRepository reportRepository) {
		this.reportRepository = reportRepository;
	}
	
	@SneakyThrows
	@Override
	public Integer insert(com.example.deepschool.Model.Report report,String UUID) {
		try {
			User user = User.fromJson(Redis.HGET(Redis.GET(UUID), "user"));
			if(!PermissionsControl.VERIFY_REPORT_INCORRECT_CONTENT(user.getPermissions())){
				throw new RuntimeException("抱歉,账号没有举报的权限.");
			}
			report.setReporterId(user.getId());
			VerifyReportAdd(report);
			return reportRepository.insert(report);
		} catch (Exception e) {
			throw new CustomException(400, "举报失败");
		}
	}
	
	@SneakyThrows
	@Override
	public Integer delete(Long id,String UUID) {
		try {
			User user = User.fromJson(Redis.HGET(Redis.GET(UUID), "user"));
			com.example.deepschool.Model.Report report = reportRepository.selectById(id);
			if(report==null){
				throw new RuntimeException("要删除的举报不存在.");
			}
			if(!Objects.equals(report.getReporterId(), user.getId())){
				throw new RuntimeException("没有权限删除举报.");
			}
			return reportRepository.delete(id);
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
	@SneakyThrows
	@Override
	public Integer update(com.example.deepschool.Model.Report report,String UUID) {
		try {
			User user = User.fromJson(Redis.HGET(Redis.GET(UUID), "user"));
			if(!PermissionsControl.VERIFY_IS_ADMIN(user.getPermissions())){
				throw new RuntimeException("没有权限修改举报的状态.");
			}
			VerifyReportUpdate(report);
			report.setHandlerId(user.getId());
			return reportRepository.update(report);
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
	@SneakyThrows
	@Override
	public com.example.deepschool.Model.Report[] select(Integer page,String UUID) {
		try {
			User user = User.fromJson(Redis.HGET(Redis.GET(UUID), "user"));
			if(!PermissionsControl.VERIFY_IS_ADMIN(user.getPermissions())){
				throw new RuntimeException("没有权限查看举报的信息.");
			}
			Pagination pagination = new Pagination();
			pagination.page(page);
			return reportRepository.select(pagination);
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
	@SneakyThrows
	@Override
	public com.example.deepschool.Model.Report selectById(Long id,String UUID) {
		try {
			User user = User.fromJson(Redis.HGET(Redis.GET(UUID), "user"));
			if(!PermissionsControl.VERIFY_IS_ADMIN(user.getPermissions())){
				throw new RuntimeException("没有权限查看举报的信息.");
			}
			return reportRepository.selectById(id);
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
	@SneakyThrows
	@Override
	public com.example.deepschool.Model.Report[] selectByReporterId(String UUID) {
		try {
			User user = User.fromJson(Redis.HGET(Redis.GET(UUID), "user"));
			return reportRepository.selectByReporterId(user.getId());
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
}
